﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.CoordCovert.model;
using System.IO;

using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Convertor;

namespace PlanningGIS.CoordCovert.form
{
    public partial class Frm4ParaTrans : Office2007Form
    {
        public Frm4ParaTrans()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "请选择需要合并的四参数文件";
            
            openFileDialog1.Multiselect = true;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.Filter = "四参数文件(*.txt)|*.txt";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] strFiles = openFileDialog1.FileNames;
                for (int i = 0; i < strFiles.Length; i++)
                {
                    string strKey = strFiles[i];
                    string fileName = System.IO.Path.GetFileNameWithoutExtension(strKey.ToUpper());

                    if (!lvwFile.Items.ContainsKey(strKey))
                    {
                        ListViewItem lvi=lvwFile.Items.Add(strKey, fileName, "");
                        lvi.Tag =strKey;
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvwFile.SelectedItems.Count > 0)
            {
                while (lvwFile.SelectedItems.Count > 0)
                {
                    lvwFile.Items.Remove(lvwFile.SelectedItems[0]);
                }
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (lvwFile.SelectedItems == null || lvwFile.SelectedItems.Count != 1) return;
     

            int sltIdx = lvwFile.SelectedIndices[0];

            if (sltIdx == 0) return;

            ListViewItem lvi = lvwFile.SelectedItems[0];

            string strKey = lvi.Tag.ToString();
            string fileName = lvi.Text;

            lvwFile.Items.RemoveAt(sltIdx);

            lvi = lvwFile.Items.Insert(sltIdx - 1, strKey, fileName, "");
            lvi.Tag = strKey;
            lvi.Selected = true;
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (lvwFile.SelectedItems == null || lvwFile.SelectedItems.Count != 1) return;
            if (lvwFile.SelectedIndices[0] == lvwFile.Items.Count - 1) return;

            ListViewItem lvi = lvwFile.SelectedItems[0];
            int sltIdx = lvwFile.SelectedIndices[0];
           
            string strKey = lvi.Tag.ToString();
            string fileName = lvi.Text;

            lvwFile.Items.RemoveAt(sltIdx);
           
            lvi = lvwFile.Items.Insert(sltIdx + 1, strKey, fileName, "");

            lvi.Tag = strKey;
            lvi.Selected = true;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            saveFileDialog1 .Title = "请设置合并之后的四参数文件路径";

            saveFileDialog1.RestoreDirectory = true;

            saveFileDialog1.Filter = "四参数文件(*.txt)|*.txt";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtSave.Text = saveFileDialog1.FileName;
            }
        }

        /// <summary>
        /// 两个四参数合并
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private Parameter4 Union(Parameter4 p1, Parameter4 p2)
        {
            Parameter4 p = new Parameter4();

            p.namta = p1.namta * p2.namta;
            p.alpa = p1.alpa + p2.alpa;

            p.a = p2.a + p2.namta * (Math.Cos(p2.alpa) * p1.a - Math.Sin(p2.alpa) * p1.b);
            p.b = p2.b + p2.namta * (Math.Sin(p2.alpa) * p1.a + Math.Cos(p2.alpa) * p1.b);

            return p;
        }

        private void Save(Parameter4 p, string fileName)
        {
            string strTextpath = fileName;
            if (File.Exists(strTextpath)) File.Delete(strTextpath);
            StreamWriter sw = File.CreateText(strTextpath);
            sw.WriteLine(p.a);
            sw.WriteLine(p.b);
            sw.WriteLine(p.alpa);
            sw.WriteLine(p.namta);
            sw.Close();
        }

        /// <summary>
        /// 从参数文件中读取四参数
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private Parameter4 GetParameter4(string fileName)
        {
            Parameter4 p4 = new Parameter4 ();

            StreamReader streamReaderr = null;
            streamReaderr = File.OpenText(fileName);

            string txt = "";
            txt = streamReaderr.ReadLine();
            if (StringHelper.CanConvert2Double(txt))
            {
                p4.a = Double.Parse(txt);
            }

            txt = streamReaderr.ReadLine();
            if (StringHelper.CanConvert2Double(txt))
            {
                p4.b = Double.Parse(txt);
            }

            txt = streamReaderr.ReadLine();
            if (StringHelper.CanConvert2Double(txt))
            {
                p4.alpa = Double.Parse(txt);
            }

            txt = streamReaderr.ReadLine();
            if (StringHelper.CanConvert2Double(txt))
            {
                p4.namta = Double.Parse(txt);
            }

            return p4;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwFile.Items.Count < 2) return;
                if (txtSave.Text == "") return;

                string fileName = lvwFile.Items[0].Tag.ToString();
                Parameter4 p = GetParameter4(fileName);

                for (int i = 1; i < lvwFile.Items.Count; i++)
                {
                    fileName = lvwFile.Items[i].Tag.ToString();
                    Parameter4 ptmp = GetParameter4(fileName);
                    p = Union(p, ptmp);
                }

                Save(p, txtSave.Text);

                MsgHelper.ShowInfomationMsgbox("四参数合并成功。");
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox("四参数合并失败：" + ex.Message);
            }
        }
    }
}
